"use strict";
require([
  'backbone'
], function (Backbone) {

  const test = Backbone.Model.extend({
    url : '/',
    initialize () {
      this.bind('change:name', function(){
        console.log(this.attributes);
      });
      this.bind('error', function(){
        console.log('数据提交失败');
      });
    },
    defaults : {
      name : '大白兔',
      lang : '汉语'
    },
    validate (obj) {
      if(obj.name.length < 2) console.log('昵称过短');
    }

  });

  const user = new test



  //console.log(user)
  //console.log(user.get("name"))
  //  user.save();

  const Users = Backbone.Collection.extend({
    initialize(){
      this.bind('add', function(obj){
        console.log('add', obj.attributes);
      });
      this.bind('remove', function(obj){
        console.log('remove', obj.attributes);
      });
      this.bind('change', function(obj){
        console.log('change', obj.attributes);
      });
    },
    model : test
  });

  const users = new Users;


  user.set({
    name : '卡恩',
    lang : '德语'
  });

  users.each(function(obj){
    console.log(obj.attributes);
  });

  //users.remove(user);

  const Userv = Backbone.View.extend({
    initialize : function(){
      _.bindAll(this, 'render');
      users.bind('add', this.render);
    },
    el : '.user',
    template : _.template($('#user_tmpl').html()),
    render : function(){
      $(this.el).append(this.template(this.model.toJSON()));
      return this;
    }
  });

  const arr = [['大白兔', '汉语'], ['贝克汉姆', '英语'], ['齐达内', '法语']];
  for(let i = 0; i < arr.length; i ++){
    let user = new test({
      name : arr[i][0],
      lang : arr[i][1]
    });
    users.add(user);
    new Userv({
      model : user
    });
  }








});

